package com.koko.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.koko.constant.MessageConstant;
import com.koko.context.AdminContext;
import com.koko.exception.AccountPermissionInsufficientException;
import com.koko.game.dto.CategoryCardDTO;
import com.koko.game.dto.CategoryDTO;
import com.koko.game.dto.CategoryPageQueryDTO;
import com.koko.game.dto.GameCategoriesDTO;
import com.koko.game.entity.Category;
import com.koko.game.entity.GameCategory;
import com.koko.game.vo.CategoryVO;
import com.koko.mapper.CategoryMapper;
import com.koko.mapper.GameCategoryMapper;
import com.koko.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private GameCategoryMapper gameCategoryMapper;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        return categoryMapper.pageQuery(categoryPageQueryDTO);
    }

    /**
     * 新增游戏分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.insert(category);
    }

    /**
     * 批量删除分类
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 验证当前账号权限
        adminPermission();
        for (Long id : ids) {
            // 1. 先删除游戏分类关系
            gameCategoryMapper.deleteByCategoryId(id);
            // 2. 再删除游戏分类
            categoryMapper.deleteById(id);
        }
    }

    /**
     * 修改分类信息
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    /**
     * 查询分类详细信息
     * @param id
     */
    @Override
    public Category getById(Long id) {
        Category category = categoryMapper.getById(id);
        return category;
    }

    @Override
    public Category getByName(String name) {
        return categoryMapper.getByName(name);
    }

    @Override
    public List<CategoryVO> getCategoryByGameId(Long gameId) {
        Page<Category> category = categoryMapper.getByGameId(gameId);
        List<CategoryVO> gameCategoryVOList = category.getResult().stream().map((item) -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(item, categoryVO);
            return categoryVO;
        }).collect(Collectors.toList());
        return gameCategoryVOList;
    }

    @Override
    @Transactional
    public void updateCategoriesInGame(GameCategoriesDTO gameCategoriesDTO) {
        // 1. 先删除原有关系
        gameCategoryMapper.deleteByGameId(gameCategoriesDTO.getGameId());
        // 2. 添加新的关系
        GameCategory gameCategory = new GameCategory();
        gameCategory.setGameId(gameCategoriesDTO.getGameId());
        for (CategoryDTO category : gameCategoriesDTO.getCategories()) {
            gameCategory.setCategoryId(category.getId());
            gameCategoryMapper.insert(gameCategory);
        }
    }

    @Override
    public List<CategoryVO> getAllCategories(String name) {
        List<Category> categories = categoryMapper.getAll(name);
        List<CategoryVO> gameCategoryVOList = categories.stream().map((item) -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(item, categoryVO);
            return categoryVO;
        }).collect(Collectors.toList());
        return gameCategoryVOList;
    }

    @Override
    @Transactional
    public void addCategoriesInGame(GameCategoriesDTO gameCategoriesDTO) {
        GameCategory gameCategory = new GameCategory();
        gameCategory.setGameId(gameCategoriesDTO.getGameId());
        for (CategoryDTO category : gameCategoriesDTO.getCategories()) {
            gameCategory.setCategoryId(category.getId());
            gameCategoryMapper.insert(gameCategory);
        }
    }

    @Override
    public List<String> getCategoryNameByGameId(Long gameId) {
        List<Category> categoryList = categoryMapper.getByGameId(gameId);
        List<String> categoryNameList = categoryList.stream().map((i) -> i.getName()).collect(Collectors.toList());
        return categoryNameList;
    }

    @Override
    public List<CategoryVO> getCategoryList(CategoryCardDTO categoryCardDTO) {
        List<Category> categoryList = categoryMapper.getCategoryList(categoryCardDTO);
        List<CategoryVO> list = categoryList.stream().map((item) -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(item, categoryVO);
            return categoryVO;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 当前管理员权限判断
     * @return
     */
    private Boolean adminPermission() {
        Integer permission = AdminContext.getCurrentStatus();
        if (permission != 0) {
            throw new AccountPermissionInsufficientException(MessageConstant.PERMISSION_INSUFFICIENT);
        }
        return true;
    }
}
